<?php
/**
 *
 * @category    API
 * @package     Api_Partner
 * @subpackage  Model
 * @author      trungpm
 */
class Api_Model_Partner_Prices {

    /*
     * Get objects by conditions author: datnh
     */
    const table_name = 'tblvgt_partners_prices';

    /*
     * Insert a new object and return key huudatxm
     */
    function insert($data) {
        $Errors = array ();
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;
        $FieldNames = '';
        $FieldValues = '';
        foreach ( $data as $key => $value ) {
            $FieldNames .= $key . ", ";
            $FieldValues .= 'N\'' . $value . '\', ';
        }
        $sql = 'INSERT INTO ' . self::table_name . ' (' . substr ( $FieldNames, 0, - 2 ) . ') ' . ' VALUES (' . substr ( $FieldValues, 0, - 2 ) . ') ';

        if (sizeof ( $Errors ) == 0) {
            DB_Query ( $sql, $db );
            return $_SESSION ['LastInsertId'];
        }
        return 0;
    }

    /*
     * update huudatxm
     */
    function update($data) {
        $Errors = array ();
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }

        $val_str = $data ['field'] . ' = \'' . $data ['value'] . '\'';

        $sql = 'UPDATE ' . self::table_name . ' SET ' . $val_str;
        $sql .= ' WHERE id = ' . $data ['id'];

        if (sizeof ( $Errors ) == 0) {
            return DB_Query ( $sql, $db );
        }
        return 0;
    }

    /*
     * delete huudatxm
     */
    function delete($id) {
        $Errors = array ();
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }

        $sql = 'DELETE FROM ' . self::table_name . ' WHERE id = ' . $id;

        if (sizeof ( $Errors ) == 0) {
            return DB_Query ( $sql, $db );
        }
        return null;
    }

    /*
     * author: datnh
     */
    function getAllPartnerPrices() {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;
        $sql = 'SELECT * FROM tblvgt_partners_prices ORDER BY partner_id, price';

        $result = DB_Query ( $sql, $db );

        if (DB_has_rows ( $result ) == 0) {
            return null;
        } else {
            return $result;
        }
        DB_free_result ( $result );
    }

    /* Verify price of partner existed. */
    function verifyPartnerPriceExisted($partnerId, $price) {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;
        $Searchsql = "SELECT count(*) as c FROM " . self::table_name . " WHERE partner_id = N'$partnerId' AND price = N'$price'";
        $SearchResult = DB_query ( $Searchsql, $db );
        $answer = DB_fetch_array ( $SearchResult );
        if ($answer ['c'] != 0) {
            return 1;
        }
        ;
        return 0;
    }

    /* Verify partner price existed. */
    function verifyPartnerPriceExistedWithField($id, $field, $value) {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;

        $sql = 'SELECT * FROM ' . self::table_name . ' WHERE id=N\'' . $id . '\'';
        $result = DB_Query ( $sql, $db );
        $rec = DB_fetch_object ( $result );
        DB_free_result ( $result );

        if ($rec) {
            $sql = '';
            if ($field == 'partner_id') {
                $sql = 'SELECT count(*) as c FROM ' . self::table_name . ' WHERE partner_id=N\'' . $value . '\' AND price = N\'' . $rec->price . '\'';
            } elseif ($field == 'price') {
                $sql = 'SELECT count(*) as c FROM ' . self::table_name . ' WHERE partner_id=N\'' . $rec->partner_id . '\' AND price = N\'' . $value . '\'';
            }

            $SearchResult = DB_query ( $sql, $db );
            $answer = DB_fetch_array ( $SearchResult );
            if ($answer ['c'] != 0) {
                return true;
            }
            return false;
        }
        return true;
    }
    function getAllPartnerPricesByPartnerId($partnerId) {
        // Check user permission
        $db = db ( $_SESSION ['UserID'], $_SESSION ['Password'] );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        ;
        $sql = 'SELECT * FROM tblvgt_partners_prices WHERE partner_id = ' . $partnerId . ' ORDER BY price';

        $result = DB_Query ( $sql, $db );

        if (DB_has_rows ( $result ) == 0) {
            return null;
        } else {
            return $result;
        }
        DB_free_result ( $result );
    }

    /*
     * author: trungpm
     */
    function GetPartnerPriceById($id, $user, $password) {
        $db = db ( $user, $password );
        if (gettype ( $db ) == 'integer') {
            $Errors [0] = NoAuthorisation;
            return $Errors;
        }
        $sql = 'SELECT * FROM tblvgt_partners_prices WHERE id=N\'' . $id . '\'';
        $result = DB_Query ( $sql, $db );
        $arrResult = DB_fetch_array ( $result );
        DB_free_result ( $result );
        return $arrResult;
    }
}

?>
